package com.chenglu.android.mine.address.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenglu.android.R;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.mine.bean.AddressListBean;


import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023 3 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：浏览记录
 */
public class AddressManagerAdapter extends BaseQuickAdapter<AddressListBean.Data, BaseViewHolder> {

    Context context;
//    List<HomeLike.Data.Records> data;
    public AddressManagerAdapter(Context context,  int layoutResId,@Nullable List<AddressListBean.Data> data) {
        super(layoutResId, data);
        this.context = context;

    }


    @Override
    protected void convert(@NonNull BaseViewHolder helper, AddressListBean.Data item) {

        if (item != null) {
//            LogUtils.v("searchadapter---" + item);
            helper.setText(R.id.tv_address_name, item.getConsignee());
            helper.setText(R.id.tv_address_phone, Utils.getStarMobile(item.getMobile()) );
            helper.setText(R.id.tv_address_all, item.getCity()+" "+item.getProvince()+"  "+item.getDistrict()+" "+
            item.getAddress());

            helper.addOnClickListener(R.id.iv_address_edit);
            if(!TextUtils.isEmpty(item.getConsignee())){
                helper.setText(R.id.tv_sub_name,item.getConsignee().substring(0,1));
            }
            if(item.getIsDefault()==1){
                helper.setGone(R.id.tv_default,true);
            }else {
                helper.setGone(R.id.tv_default,false);
            }
            helper.setText(R.id.tv_address_delete,"删除").addOnClickListener(R.id.tv_address_delete);


        }


    }


    /**
     * 参数：maxLines 要限制的最大行数
     * 参数：content  指TextView中要显示的内容
     */
    public void setMaxEcplise(final TextView mTextView, final int maxLines, final String content) {

        ViewTreeObserver observer = mTextView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mTextView.setText(content);
                if (mTextView.getLineCount() > maxLines) {
                    int lineEndIndex = mTextView.getLayout().getLineEnd(maxLines - 1);
                    //下面这句代码中：我在项目中用数字3发现效果不好，改成1了
                    String text = content.subSequence(0, lineEndIndex - 3) + "...";
                    mTextView.setText(text);
                }
                else {
                    removeGlobalOnLayoutListener(mTextView.getViewTreeObserver(), this);
                }
            }
        });
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    private void removeGlobalOnLayoutListener(ViewTreeObserver obs, ViewTreeObserver.OnGlobalLayoutListener listener) {
        if (obs == null)
            return;
        if (Build.VERSION.SDK_INT < 16) {
            obs.removeGlobalOnLayoutListener(listener);
        }
        else {
            obs.removeOnGlobalLayoutListener(listener);
        }
    }
}
